package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Employee;
import com.gaohe.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmpController {
    @Autowired
    private EmpService empService;

    //    定义登陆实现login方法
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        //数据库查询并做匹配，判断是否登陆成功
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper();
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee one = empService.getOne(lqw);
        //判断登录是否成功
        if (one == null) {
            return R.error("用户不存在");
        }

        //对密码进行md5加密
        String password = employee.getPassword();
        String s = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));

        if (!one.getPassword().equals(s)) {
            return R.error("密码错误");
        }

        //     添加session
        HttpSession session = request.getSession();
        session.setAttribute("employee", one);

        return R.success(employee);


    }


    //    定义实现登出功能的方法
    @PostMapping("/logout")
    public R<Employee> logOut() {
        return R.success(null);
    }


    //    实现分页查询功能
    @GetMapping("/page")
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name) {
//        log.info("当前页:{},每页条数:{}", page, pageSize);
        Page<Employee> page1 = new Page(page, pageSize);
//        模糊查询
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper();
//        不为空的条件
        lqw.like(StringUtils.isNotEmpty(name), Employee::getUsername, name);
//        设置排序条件
//        lqw.orderByAsc(Employee::getUpdateTime);
        empService.page(page1, lqw);
        return R.success(page1);
    }

    //    新增员工数据
    @PostMapping
    public R<String> addEmp(HttpServletRequest httpServletRequest, @RequestBody Employee employee) {
//        log.info("employee:{}",employee);
        //设置初始密码并进行md5加密
        String password = DigestUtils.md5DigestAsHex("12345".getBytes(StandardCharsets.UTF_8));
        employee.setPassword(password);
//        //设置创建与更新时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        //设置修改人与创立人
//        Employee employee1 = (Employee) httpServletRequest.getSession().getAttribute("employee");
//        log.info("employee1:{}", employee1);
//        employee.setCreateUser(employee1.getId());
//        employee.setUpdateUser(employee1.getId());
        boolean save = empService.save(employee);
        if (save) {
            return R.success("成员添加成功");
        } else {
            return R.error("成员添加失败");
        }

    }

    //    删除：实现账号禁用
    @PutMapping
    public R<String> deleteEmp(HttpServletRequest httpServletRequest, @RequestBody Employee employee) {
//        log.info("employee:{}", employee);
        /*//设置修改人与修改时间
        Employee employee1 = (Employee) httpServletRequest.getSession().getAttribute("employee");
        employee.setUpdateUser(employee1.getId());
        employee.setUpdateTime(LocalDateTime.now());*/
        boolean updateById = empService.updateById(employee);
        if (updateById) {
            return R.success("禁用成功");
        } else {
            return R.error("禁用失败");
        }

    }

    //数据回显
    @GetMapping("/{id}")
    public R<Employee> displayEmp(@PathVariable("id") String id) {
        Employee employee = empService.getById(id);
        if (employee != null) {
            return R.success(employee);
        } else {
            return R.error("没有查询到此员工");
        }

    }


}
